<template>
	<view class="container">
		<view class="stickyClass">
			<v-tabhead
				@barTap="barTap"
				:headItems="headItems"
				:activeIndex="activeIndex"
			></v-tabhead>
		</view>
		<swiper @change="changeTab"  :current="activeIndex"  class="homeSwiperContainer">
			<swiper-item class="homeSwiperContainer"  v-for="(item,index) in headItems" :key="index">
				<v-combo @closeRefresh="closeRefresh" :refresh="refresh"  :currentCode="headItems[activeIndex].code" :currentIndex='activeIndex' :code="item.code" />
			</swiper-item>
		</swiper>
	</view>
</template>
<script>
/*
    测试装饰器
     */
import mixins from '@/mixins/index';
import {mapState, mapGetters, mapMutations, mapActions} from 'vuex';

import messageBadgeEvent from '@/mixins/event/messageBadgeEvent';

import HomeService from '@/services/homepage/index';
import VTabhead from './fragment/vtabhead/index';
import VCombo from './fragment/vcombo/index'
import updateMixins from '@/mixins/update'
import {isEmpty} from '@/utils/index'
import {readFile,deleteFile} from '@/utils/fileutil';

const homeService = new HomeService();

export default {
	mixins: [mixins,messageBadgeEvent,updateMixins],
	components: {
		VTabhead,
		VCombo,
	},
	data() {
		return {
			tabHeight:40,
			activeIndex:0,
			headItems:[],
			messageBadgeIndex:2,
			refresh:false,
			appShow:true,//true:app在前台,flase:app在后台
			locateTime:0,//重复定位次数(后台定位是不计次数)，连续3次定位失败，退出应用
			indexTimeout:undefined,
			failTooMany:false,
			locations:[]
		};
	},
	
	methods: {
		catchTouchMove(){
			return false;
		},
		...mapMutations({
			setAscription:'msgStore/setAscription'
		}),
		showUpdate(){
			//#ifdef APP-PLUS
			const update = uni.getSubNVueById('update');
			update.show('slide-in-bottom',300,function(){
				console.log('show over');
			});
			//#endif
		},
		loadData(callback) {
			const $this = this;
			// #ifdef APP-PLUS  
			const platform =  uni.getSystemInfoSync().platform;
			uni.getLocation({
                    type: 'gcj02',
                    success: function (res) {
						// console.log('res:',res)
						$this.locateTime = 0;
                        const {longitude,latitude} = res;
						$this.locations.push({
							longitude,latitude
						})
						homeService.headpanel({longitude,latitude}).then(resultDto => {
							if (resultDto.success) {
								if(!$this.judgeEquals($this.headItems,resultDto.result)){
									$this.headItems = resultDto.result;
									if($this.headItems.length==1){
										this.activeIndex = 0;
										uni.showModal({
										    content: '你已离开学校范围，只保留执法资格权限',
											showCancel:false,
											confirmText:'我知道了',
										});
									}
									$this.changeAscription();
									$this.callbackSugar(callback);
								};
							};
							const timestamp = isEmpty(resultDto.timestamp)?0:resultDto.timestamp;
							console.log('=====location again:',timestamp);
							if(!$this.failTooMany){
								$this.indexTimeout = setTimeout(()=>{$this.loadData();},timestamp * 1000)
							}
						});
					},
					fail:function(){
							if($this.appShow){
								$this.locateTime++;
								if($this.locateTime<3){
									$this.loadData();
									return;
								}
							}else{//应用在后台
								return;
							}
							uni.showModal({
							title: '提示',
							showCancel:false,
							content: '获取定位信息异常，请检查应用是否开启定位，应用需要您去开启定位或激活定位权限！',
							confirmText:'我知道了',
							// success: function (res) {
							// 	switch(platform){
							// 		case 'android':
							// 		   plus.runtime.quit();
							// 		   break;
							// 		case 'ios':
							// 		   const threadClass = plus.ios.importClass("NSThread");
							// 		   const mainThread = plus.ios.invoke(threadClass, "mainThread");
							// 		   plus.ios.invoke(mainThread, "exit");
							// 		   break;
							// 	}
							// }
						});
					}
				});
			// #endif	
			// #ifdef H5
			homeService.headpanel({longitude:'118.77745032390452',latitude:'32.08294528300839'}).then(resultDto => {
				if (resultDto.success) {
					$this.headItems = resultDto.result;
					$this.changeAscription();
					$this.callbackSugar(callback);
				}
			});
			// #endif
		},
		barTap(activeIndex) {
			this.activeIndex = activeIndex;
			this.changeAscription();
		},
		changeAscription(){
			//设置当前归属，供收集学习时长时使用
			console.log('ascription',this.headItems[this.activeIndex].code)
			this.setAscription({
				ascription:this.headItems[this.activeIndex].code
			});
		},
		changeTab(e){
		    let index = e.target.current;
		    this.activeIndex = index;
		},
		closeRefresh(){
			this.refresh = false;
		},
		judgeEquals(arr1,arr2){
			if(arr1.length!=arr2.length) return false;
			for(var index in arr1){
				if(arr1[index].code!=arr2[index].code){
					return false;
				}
			}
			return true;
		}
	},
	// #ifndef MP
	// 标题栏input搜索框点击
	onNavigationBarSearchInputClicked(e) {
		//this.api.msg('点击了搜索框');
		uni.navigateTo({
			url: '/pages/search/search'
		});
	},
	//点击导航栏 buttons 时触发
	onNavigationBarButtonTap(e) {
		const index = e.index;
		console.log('index:',index)
		if (index === 2) {
			this.clickMessage();
		}else if(index === 3){
			 uni.navigateTo({
			    url: '/pages/search/search'
			});
		}
	},
	// #endif
	onPullDownRefresh() {
		//监听下拉刷新动作的执行方法，每次手动下拉刷新都会执行一次
		//this.activeIndex = 0;
		this.loadData(() => {this.activeIndex = 0;this.refresh = true;uni.stopPullDownRefresh();});
	},
	onUnload(){
		//取消定时器
		if(this.indexTimeout!=undefined){
			clearTimeout(this.indexTimeout);
		}
		uni.$off('onUpdateExam');
		uni.$off('appShow');
		uni.$off('appHide');
		uni.$off('failTooMany');
	},
	onLoad(){
		this.checkUpdate();
		uni.$on('appShow',()=>{
			this.appShow = true;
		});
		uni.$on('appHide',()=>{
			this.appShow = false;
		});
		uni.$on('failTooMany',()=>{
			console.log('=====failTooMany');
			this.failTooMany = true;
			if(this.indexTimeout!=undefined){
				clearTimeout(this.indexTimeout);
			}
		});
	},
	onReady() {
		this.loadData();
		// #ifdef APP-PLUS  
		// 修改按钮上的角标  
		// index: 按钮索引, text: 角标文本内容  
		let messageBadge_ = '0'
		if(this.messageBadge!=''){
			messageBadge_ = this.messageBadge;
		}
		// #endif
	}
};
</script>

<style lang="scss" src="./index.scss"></style>
<style lang="scss">
	.homeSwiperContainer {
		width: 100%;
		height: calc(100vh - 80upx);
		flex: 1;
		overflow-y:scroll;
		background-color: #fff;
	}
	uni-swiper-item {
		overflow-y: scroll;
	}
	
</style>